// by Xeno
//#define __DEBUG__
#define THIS_FILE "fn_arifire.sqf"
#include "x_setup.sqf"
private ["_height", "_type", "_radius", "_arix", "_ariy", "_ang", "_posf", "_posb", "_posl", "_posr", "_series", "_x1", "_y1", "_angle", "_strenght", "_i", "_j", "_nos", "_shell", "_xo", "_pos", "_pod", "_obj", "_center_x", "_center_y", "_wp_array", "_ari_target","_ari_avail","_side_arti", "_num_arti", "_aristr", "_topicside", "_topicside2", "_logic", "_ari_vecs"];
if (!isServer) exitWith {};

PARAMS_6(_ari_type,_ari_salvos,_arti_operator,_ari_target,_ari_avail,_num_arti);
_side_arti = side (group _arti_operator);

__TRACE_3("","_ari_type","_ari_salvos","_arti_operator");
__TRACE_3("","_ari_target","_ari_avail","_num_arti");

if !(X_JIPH getVariable _ari_avail) exitWith {};

[_ari_avail,false] call FUNC(NetSetJIP);

sleep 9.123;

#ifndef __TT__
_aristr = switch (_num_arti) do {
	case 1: {(localize "STR_DOM_MISSIONSTRING_934")};
	case 2: {(localize "STR_DOM_MISSIONSTRING_935")};
};
GVAR(kb_logic1) kbTell [_arti_operator,GVAR(kb_topic_side_arti),"ArtilleryRoger",["1","",_aristr,[]],"SIDE"];
#else
_topicside = switch (_num_arti) do {
	case 1: {"HQ_ART_W"};
	case 2: {"HQ_ART_E"};
};
_topicside2 = switch (_num_arti) do {
	case 1: {"HQ_W"};
	case 2: {"HQ_E"};
};
_logic = switch (_num_arti) do {
	case 1: {GVAR(hq_logic_en1)};
	case 2: {GVAR(hq_logic_ru1)};
};
_logic kbTell [_arti_operator,_topicside,"ArtilleryRoger",["1","",(localize "STR_DOM_MISSIONSTRING_936"),[]],"SIDE"];
#endif
sleep 1;
#ifndef __TT__
_aristr = switch (_num_arti) do {
	case 1: {(localize "STR_DOM_MISSIONSTRING_937")};
	case 2: {(localize "STR_DOM_MISSIONSTRING_938")};
};
["ArtilleryUnAvailable", _aristr] call FUNC(KBSendMsgAll);
#else
[_topicside2,"ArtilleryUnAvailable", (localize "STR_DOM_MISSIONSTRING_939"), ["BeAdvisedArtilleryIsUnavailableAtThisTimeOut"]] call FUNC(KBSendMsgAll);
#endif
sleep 8.54;
_name_shell = getText(configFile/"CfgMagazines"/_ari_type/"displayname");
#ifndef __TT__
GVAR(kb_logic1) kbTell [_arti_operator,GVAR(kb_topic_side_arti),"ArtilleryExecute",["1","",_aristr,[]],["2","",_name_shell,[]],["3","",str(_ari_salvos),[]],"SIDE"];
#else
_logic kbTell [_arti_operator,_topicside,"ArtilleryExecute",["1","",(localize "STR_DOM_MISSIONSTRING_939"),[]],["2","",_name_shell,[]],["3","",str(_ari_salvos),[]],"SIDE"];
#endif

sleep 8 + (random 7);

_ari_vecs = [];
for "_i" from 0 to 20 do {
	_vvx = missionNamespace getVariable format ["d_arty%1_%2", _num_arti, _i];
	if (!isNil "_vvx") then {
		_ari_vecs set [count _ari_vecs, _vvx];
	};
};

if (count _ari_vecs == 0) exitWith {
	[_ari_avail, true] call FUNC(NetSetJIP);
};

_at_pos = position _ari_target;
_at_pos set [2,0];
__TRACE_2("","_ari_vecs","_at_pos");
_eta_time = (_ari_vecs select 0) getArtilleryETA [_at_pos, _ari_type];

_inrange = _at_pos inRangeOfArtillery [[(_ari_vecs select 0)], _ari_type];

__TRACE_2("","_eta_time","_inrange");

_magammoconf = getText(configFile/"CfgMagazines"/_ari_type/"ammo");
_ammoconf = configFile/"CfgAmmo"/_magammoconf;
_is_flare = getText(_ammoconf/"effectFlare") == "CounterMeasureFlare";
_is_smoke = getText(_ammoconf/"submunitionAmmo") == "SmokeShellArty";

_enemy_units = [];
_soldier_type = switch (GVAR(enemy_side)) do {
	case "OPFOR": {"SoldierEB"};
	case "BLUFOR": {"SoldierWB"};
	case "INDEPENDENT": {"SoldierGB"};
};

for "_series" from 1 to _ari_salvos do {
	{
		_radius = 20 + random 20;
		_angle = floor random 360;
		_x1 = (_at_pos select 0) - ((random _radius) * sin _angle);
		_y1 = (_at_pos select 1) - ((random _radius) * cos _angle);
		
		_x setVehicleAmmo 1;
		_x setFuel 1;
		_x setDamage 0;
		
		_x doArtilleryFire [[_x1, _y1, 0], _ari_type, 1];
		sleep 0.2;
	} forEach _ari_vecs;
	
	#ifndef __TT__
	_kbstr = switch (_num_arti) do {
		case 1: {"ArtilleryFirstOTW"};
		case 2: {"ArtillerySecondOTW"};
	};
	GVAR(kb_logic1) kbTell [_arti_operator,GVAR(kb_topic_side_arti),_kbstr,["1","",str(_series),[]],["2","",str(round(_eta_time)),[]],"SIDE"];
#else
	_logic kbTell [_arti_operator,_topicside,"ArtilleryOTW",["1","",str(_series),[]],["2","",str(round(_eta_time)),[]],"SIDE"];
#endif

	sleep _eta_time;
	
	#ifndef __TT__
	_kbstr = switch (_num_arti) do {
		case 1: {"ArtilleryFirstSplash"};
		case 2: {"ArtillerySecondSplash"};
	};
	GVAR(kb_logic1) kbTell [_arti_operator,GVAR(kb_topic_side_arti),_kbstr,["1","",str(_series),[]],"SIDE"];
#else
	_logic kbTell [_arti_operator,_topicside,"ArtillerySplash",["1","",str(_series),[]],"SIDE"];
#endif

	if (!_is_flare && {!_is_smoke}) then {
		_nos = [getPosASL _ari_target select 0,getPosASL _ari_target select 1,0] nearEntities [_soldier_type, 40];
		{if !(_x in _enemy_units) then {_enemy_units set [count _enemy_units, _x]}} forEach _nos;
	};

	if (_series < _ari_salvos) then {
#ifndef __TT__
		GVAR(kb_logic1) kbTell [_arti_operator,GVAR(kb_topic_side_arti),"ArtilleryReload",["1","",_aristr,[]],"SIDE"];
#else
		_logic kbTell [_arti_operator,_topicside,"ArtilleryReload",["1","",(localize "STR_DOM_MISSIONSTRING_939"),[]],"SIDE"];
#endif
		sleep (GVAR(arti_reload_time) + random 3);
	};
};

sleep 3;

_arti_operator addScore ({!alive _x} count _enemy_units);
_enemy_units = nil;
sleep 0.5;

#ifndef __TT__
GVAR(kb_logic1) kbTell [_arti_operator,GVAR(kb_topic_side_arti),"ArtilleryComplete",["1","",_aristr,[]],"SIDE"];
#else
_logic kbTell [_arti_operator,_topicside,"ArtilleryComplete",["1","",(localize "STR_DOM_MISSIONSTRING_939"),[]],"SIDE"];
#endif

#ifndef __TT__
[_ari_salvos, _ari_type, _ari_avail,_aristr] spawn {
#else
[_ari_salvos, _ari_type, _ari_avail,_aristr,_topicside2] spawn {
#endif
	scriptName "spawn_x_arifire_artiavailable";
	private ["_ari_salvos", "_ari_type", "_ari_avail", "_topicside2"];
	PARAMS_4(_ari_salvos,_ari_type,_ari_avail,_aristr);
#ifdef __TT__
	_topicside2 = _this select 4;
#endif
	sleep (GVAR(arti_available_time) + ((_ari_salvos - 1) * 200)) + (random 60) + (if (GVAR(MissionType) != 2) then {if (_ari_type == "sadarm") then {180} else {0}} else {300});
	[_ari_avail,true] call FUNC(NetSetJIP);
#ifndef __TT__
	["ArtilleryAvailable", _aristr] call FUNC(KBSendMsgAll);
#else
	[_topicside2,"ArtilleryAvailable", (localize "STR_DOM_MISSIONSTRING_939"), ["ArtilleryAvailableTransmitTargetLocationOver"]] call FUNC(KBSendMsgAll);
#endif
};
